import { Router } from "../../utils/common.js";
const db = wx.cloud.database();
Page({
  data: {
    isMyFeed: true,
    myFeedList: [],
    feedMyList: [],
    pageMyFeedList: 1,
    pageFeedMyList: 1,
    totalCountMyFeedList: 0, //数据总数
    totalCountFeedMyList: 0,
    showMyFeedNoData: false, // 展示我的遛狗记录Nodata
    showFeedMyNoData: false, // 展示我的遛狗记录Nodata
    loadProgress: 0
  },

  onLoad() {
    // 我的遛狗记录
    this.getTotalCountMyFeedList();
    this.getMyFeedList();
    // 遛我狗子记录
    this.getFeedMyList();
    this.getTotalCountFeedMyDogList();
  },
  // loading动画
  loadProgress() {
    this.setData({
      loadProgress: this.data.loadProgress + 3
    })
    if (this.data.loadProgress < 100) {
      setTimeout(() => {
        this.loadProgress();
      }, 100)
    } else {
      this.setData({
        loadProgress: 0
      })
    }
  },
  /**
 * 获取我的遛狗记录总数
 */
  getTotalCountMyFeedList() {
    let that = this;
    db.collection('p_pet_run').where({
      userid: wx.getStorageSync('openid')
    }).count({
      success: function (res) {
        that.setData({
          totalCountMyFeedList: res.total
        });
        if (res.total == 0) {
          that.setData({
            showMyFeedNoData: true
          });
        }
      }
    })
  },
  /**
   * 获取我的遛狗记录
   */
  getMyFeedList() {
    db.collection('p_pet_run').where({
      userid: wx.getStorageSync('openid')
    }).orderBy('createTime', 'desc').skip(0)
      .limit(10)
      .get({
        success: res => {
          this.setData({
            myFeedList: res.data
          })
        },
        fail: err => {
          console.log('[数据库] [查询记录] 失败：');
        }
      })
  },
  /**
* 获取遛我狗子记录总数
*/
  getTotalCountFeedMyDogList() {
    let that = this;
    db.collection('p_pet_run').where({
      petUserid: wx.getStorageSync('openid')
    }).count({
      success: function (res) {
        console.log(res.total);
        that.setData({
          totalCountFeedMyList: res.total
        });
        if (res.total == 0) {
          that.setData({
            showMyFeedNoData: true
          });
        }
      }
    })
  },
  /**
 * 获取遛我狗子记录列表
 */
  getFeedMyList() {
    db.collection('p_pet_run').where({
      petUserid: wx.getStorageSync('openid')
    }).orderBy('createTime', 'desc').skip(0)
      .limit(10)
      .get({
        success: res => {
          this.setData({
            feedMyList: res.data
          })
        },
        fail: err => {
          console.log('[数据库] [查询记录] 失败：');
        }
      })
  },
  /**
 * 页面上拉触底事件的处理函数
 */
  onReachBottom: function () {
    if (this.data.isMyFeed) {
      // 我的遛狗记录列表
      this.getMyFeedDogList();
    } else {
      // 溜我狗子记录列表
      this.getFeedMyDogList();
    }

  },
  getMyFeedDogList() {
    this.loadProgress();
    let page = this.data.pageMyFeedList;
    if (this.data.myFeedList.length < this.data.totalCountMyFeedList) {
      db.collection('p_pet_run').where({
        userid: wx.getStorageSync('openid')
      }).orderBy('createTime', 'desc').skip(page * 10)
        .limit(10)
        .get({
          success: res => {
            let myFeedList = this.data.myFeedList;
            for (let i = 0; i < res.data.length; i++) {
              myFeedList.push(res.data[i]);
            }
            this.setData({
              myFeedList: myFeedList,
              pageMyFeedList: page + 1,
              loadProgress: 100
            })
          },
          fail: err => {
            console.log('[数据库] [查询记录] 失败：');
          }
        })

    } else {
      this.setData({
        loadProgress: 100
      });
      wx.showToast({
        title: '没有更多数据了',
      })
    }
  },
  getFeedMyDogList() {
    this.loadProgress();
    let page = this.data.pageFeedMyList;
    if (this.data.feedMyList.length < this.data.totalCountFeedMyList) {
      db.collection('p_pet_run').where({
        userid: wx.getStorageSync('openid')
      }).orderBy('createTime', 'desc').skip(page * 10)
        .limit(10)
        .get({
          success: res => {
            let feedMyList = this.data.feedMyList;
            for (let i = 0; i < res.data.length; i++) {
              feedMyList.push(res.data[i]);
            }
            this.setData({
              feedMyList: feedMyList,
              pageFeedMyList: page + 1,
              loadProgress: 100
            })
          },
          fail: err => {
            console.log('[数据库] [查询记录] 失败：');
          }
        })

    } else {
      this.setData({
        loadProgress: 100
      });
      wx.showToast({
        title: '没有更多数据了',
      })
    }
  },
  clickTab: function () {
    let that = this
    that.setData({
      isMyFeed: !that.data.isMyFeed
    })
  },
  navToPetDet: function (e) {
    let params = {
      petId: e.currentTarget.dataset.petid
    }
    Router.navigateTo("../dogdet/dogdet", params);
  },
  navToSomeonePet: function (e) {
    let currUserId = e.currentTarget.dataset.userid
    if (!currUserId == wx.getStorageSync('openid')) {
      wx.switchTab({
        url: "/pages/index/index"
      })
    } else {
      let params = {
        openid: currUserId
      }
      Router.navigateTo("../doglist/doglist", params)
    }
  }

})